package com.itheima.chapter08.service;

import com.itheima.chapter08.domain.User;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

@Service
public class RabbitMQService {
//    @RabbitListener(queues = "fanout_queue_email")
//    public void psubConsumerEmail(Message message) {
//        byte[] body = message.getBody();
//        String s = new String(body);
//        System.out.println("邮件业务收到消息：" + s);
//    }
//
//    @RabbitListener(queues = "fanout_queue_sms")
//    public void psubConsumerSms(Message message) {
//        byte[] body = message.getBody();
//        String s = new String(body);
//        System.out.println("短信业务收到消息：" + s);
//    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("fanout_queue_email"),
            exchange = @Exchange(value = "fanout_exchange", type = "fanout")))
    public void psubConsumerEmailAno(User user) {
        System.out.println("邮件业务受到消息：" + user);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("fanout_queue_sms"),
            exchange = @Exchange(value = "fanout_exchange", type = "fanout")))
    public void psubConsumerSmsAno(String message) {
        System.out.println("手机业务受到消息：" + message);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("routing_queue_error"),
            exchange = @Exchange(value = "routing_exchange", type = "direct"),
            key = "error_routing_key")
    )
    public void routingConsumerError(String message) {
        System.out.println("收到的error级别日志消息：" + message);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("routing_queue_email"),
            exchange = @Exchange(value = "routing_exchange", type = "direct"),
            key = {"error_routing_key", "info_routing_key", "warning_routing_key"})
    )
    public void routingConsumerAll(String message) {
        System.out.println("收到的info/error/warning级别日志消息：" + message);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("topic_queue_error"),
            exchange = @Exchange(value = "topic_exchange", type = "topic"),
            key = "info.#.email.#")
    )
    public void topicConsumerEmail(String message) {
        System.out.println("收到的邮件订阅需求处理消息：" + message);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("topic_queue_sms"),
            exchange = @Exchange(value = "topic_exchange", type = "topic"),
            key = "info.#.sms.#")
    )
    public void topicConsumerSms(String message) {
        System.out.println("收到的短信订阅需求处理消息：" + message);
    }

}
